1382A - Common Subsequence - CodeForces Solution


brute force *800

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    n,m=map(int,input().split())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    done=False
    for e in a :
        if e in b:
            print("YES")
            print(1,e)
            done=True
            break
    if not done:
        print("NO")

C++ Code:

#include <iostream>
#include<algorithm>
#include<vector>
#include<iomanip>
using namespace std;
int main()
{
	int t, m, n;
	cin >> t;
	while (t--)
	{
		int arr[1005] = {},x,ans;
		cin >> n >> m;
		bool c = 0;
		for (int i = 0; i < n; i++)
		{
			cin >> x;
			arr[x]++;
		}
		for (int i = 0; i < m; i++)
		{
			cin >> x;
			if (arr[x])
			{
				ans = x;
				c = 1;
			}

		}
		if (c)
			cout << "YES" << endl << 1 << " " << ans << endl;
		else
			cout << "NO" << endl;

	}

	return 0;
}		


Comments

Submit
0 Comments
More Questions

84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship